package top.dancun.easstudent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.Query;

import top.dancun.common.utils.R;
import top.dancun.easstudent.dao.EmploymentDestinationDao;
import top.dancun.easstudent.dao.StudentDao;
import top.dancun.easstudent.entity.EmploymentDestinationEntity;
import top.dancun.easstudent.entity.StudentEntity;
import top.dancun.easstudent.feign.BaseInfoFeign;
import top.dancun.easstudent.service.EmploymentDestinationService;


@Service("employmentDestinationService")
public class EmploymentDestinationServiceImpl extends ServiceImpl<EmploymentDestinationDao, EmploymentDestinationEntity> implements EmploymentDestinationService {

    @Autowired
    private StudentDao studentDao;

    @Override
    public PageUtils queryPage(Map<String, Object> pageinfo,Map<String,String > params) {

        IPage<EmploymentDestinationEntity> page = new Query<EmploymentDestinationEntity>().getPage(pageinfo);
        LambdaQueryWrapper<EmploymentDestinationEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StringUtils.isNotBlank(params.get("studentId")),
                        EmploymentDestinationEntity::getStudentId,
                        params.get("studentId"))
                .eq(StringUtils.isNotBlank(params.get("type")),
                        EmploymentDestinationEntity::getType,
                        params.get("type"));
        //查询班级
        if (StringUtils.isNotBlank(params.get("cId"))) {
            List<StudentEntity> studentEntityList = studentDao.selectList(Wrappers.lambdaQuery(StudentEntity.class).eq(StudentEntity::getCid, params.get("cId")));
            List<String> studentIdList = new ArrayList<>();
            studentEntityList.forEach(studentEntity -> {
                studentIdList.add(studentEntity.getSid());
            });
            wrapper.in(EmploymentDestinationEntity::getStudentId, studentIdList.size()==0? Arrays.asList("000") :studentIdList);
        }

        page = this.baseMapper.listPage(wrapper,page);
        page.getRecords().forEach(employmentDestinationEntity -> {
            try{
                employmentDestinationEntity.setName(studentDao.selectById(employmentDestinationEntity.getStudentId()).getName());
            }catch (Exception e){
                employmentDestinationEntity.setName("获取姓名错误，学号："+employmentDestinationEntity.getStudentId());
            }
        });
        return new PageUtils(page);
    }

    @Override
    public EmploymentDestinationEntity getED(String studentId) {
        EmploymentDestinationEntity employmentDestination = lambdaQuery()
                .eq(EmploymentDestinationEntity::getStudentId,studentId)
                .one();
        if(Objects.isNull(employmentDestination)){
            return new EmploymentDestinationEntity();
        }
        try{
            employmentDestination.setName(studentDao.selectById(employmentDestination.getStudentId()).getName());
        }catch (Exception e){
            employmentDestination.setName("获取姓名错误，学号："+employmentDestination.getStudentId());
        }
        return employmentDestination;
    }

    @Override
    public EmploymentDestinationEntity getById(Serializable id) {
        EmploymentDestinationEntity employmentDestination = super.getById(id);
        try {
            employmentDestination.setName(studentDao.selectById(employmentDestination.getStudentId()).getName());
        } catch (Exception e) {
            employmentDestination.setName("获取姓名错误，学号：" + employmentDestination.getStudentId());

        }
        return employmentDestination;
    }
}